<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData" :rowCls="rowCls">
    <template v-if="!taskCode" v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft="{ selected = [] }">
      <q-btn :disable="isDisable(selected)" outline text-color="primary" unelevated
             @click="handleComplete(selected[0])">
        {{ $t('TaskCore.ManualComplete') }}
        <q-icon class="on-right" color="warning" name="mdi-information-variant" size="1.25rem">
          <q-tooltip v-bind="$tooltipAttr">{{ $t('TaskCore.ManualCompleteTip1') }}</q-tooltip>
        </q-icon>
      </q-btn>
      <q-btn :disable="isDisable(selected)" outline text-color="primary" unelevated
             @click="handleManualNotify(selected[0])">
        {{ $t('TaskCore.ManualNotification') }}
        <q-icon class="on-right" color="warning" name="mdi-information-variant" size="1.25rem">
          <q-tooltip v-bind="$tooltipAttr">{{ $t('TaskCore.ManualNotificationTip1') }}</q-tooltip>
        </q-icon>
      </q-btn>
      <q-btn :disable="isDisable(selected)" outline text-color="primary" unelevated @click="handleLogs(selected[0])">
        {{ $t('LogCore.NotificationLog') }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickTips') }}</q-tooltip>
      </q-icon>
    </template>
    <template v-slot:handleExtraParam="{ row }">
      <QCodePrism :code="row.handleExtraParam" classes="ellipsis-3-lines overflow-hidden no-padding"></QCodePrism>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="`${$t('Misc.SpecifyDetails', { name: $t('TaskCore.SubTask') })}`"
           :menuBtn="false" :persistent="false" width="750">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="3">
      <template v-slot:handleExtraParam="{ row }">
        <QLink classes="multiRow_2 text-caption non-selectable multipoint-params"
               @click="handleJsonPopup(row.handleExtraParam)">
          {{ row.handleExtraParam }}
        </QLink>
      </template>
    </QInfo>
  </q-modal>
  <q-modal v-model="logVisible" :header="`${$t('LogCore.NotificationLog')}`" :menuBtn="false" :persistent="false"
           minWidth="680" nonScrollArea width="50%">
    <TrafficControlNotificationLogs :rowInfo="rowInfo"></TrafficControlNotificationLogs>
  </q-modal>
  <JsonPopup v-model="popupVisible" :jsonInfo="jsonInfo" :targetEl="targetEl"></JsonPopup>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { postAPI } from 'src/api/index.js'
import { getDictData } from 'src/enum/ajaxRemoteData.js'
import { createDialog } from 'src/utils/utils.js'
import {
  notificationObjects, notificationStatus, notificationTargetTypes, sitePositionTypes
} from 'src/views/TaskCore/enum.js'
import TrafficControlNotificationLogs from 'src/views/TaskCore/TrafficControlNotificationLogs.vue'
import { computed, onMounted, ref } from 'vue'

const props = defineProps({ taskCode: String })

const rowInfo = ref({})
const jsonInfo = ref('')
const tcrModes = ref([])
const searchForm = ref({})
const targetEl = ref(false)
const logVisible = ref(false)
const popupVisible = ref(false)
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/execL3TcrNotify/list' })
getDictData('AGV_TRAFFIC_CTRL_MODE').then(data => (tcrModes.value = data))
const isDisable = computed(() => selected => {
  if (!selected || selected.length !== 1) return true
  return selected[0].sysInner === '1'
})
const tableOption = computed(() => {
  return {
    span: 22, export: true, labelWidth: 80, searchSpan: 4, selection: 'single', maxHeight: 'inherit',
    column: [{
      label: `${i18n.t('TaskCore.NotificationTime')}`, field: 'createdTime', minWidth: 80, search: false
    }, {
      label: `${i18n.t('Misc.TrafficScenario')}`, field: 'tcrCode', minWidth: 80
    }, {
      label: `${i18n.t('Misc.ControlMode')}`, field: 'tcrModeCode', type: 'select', options: tcrModes.value,
      minWidth: 80, props: { label: toI18nName('labelZhCn'), value: 'value' }
    }, {
      label: `${i18n.t('TaskCore.TaskGroupCode')}`, field: 'taskGroupCode', minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('TaskCore.TaskCode')}`, field: 'parentTaskCode', minWidth: 80, search: false
    }, {
      label: `${i18n.t('TaskCore.SubTaskCode')}`, field: 'taskCode', minWidth: 80
    }, {
      label: `${i18n.t('TaskCore.NotificationStatus')}`, field: 'notifyStatus', type: 'select',
      options: notificationStatus.value, minWidth: 80, useTag: true
    }, {
      label: `${i18n.t('MapCore.TrafficControlPoint')}`, field: 'ctrPointCode', minWidth: 80, search: false
    }, {
      label: `${i18n.t('MapCore.StationPositionType')}`, field: 'positionType', options: sitePositionTypes.value,
      minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('Order')}`, field: 'stepNumber', minWidth: 80, search: false,
      hide: true
    }, {
      label: `${i18n.t('RCSCore.InterfaceName')}`, field: toI18nName('sdaiNameZhCn'), minWidth: 80,
      search: false, hide: true
    }, {
      label: `${i18n.t('LogCore.ReqParams')}`, field: 'handleExtraParam', minWidth: 80, search: false,
      hide: true, slot: true
    }, {
      label: `${i18n.t('Misc.ExecTiming')}`, field: 'notifyMethodType', minWidth: 80, search: false,
      hide: true
    }, {
      label: `${i18n.t('TaskCore.NotificationTarget')}`, field: 'notifyTarget', minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('TaskCore.NotificationTargetType')}`, field: 'notifyTargetType',
      options: notificationTargetTypes.value, minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('Misc.RetryCount')}`, field: 'notifyRetryCnt', minWidth: 80, search: false,
      defaultValue: '0'
    }, {
      label: `${i18n.t('StartTime')}`, field: 'notifyStartTime', minWidth: 80, search: false
    }, {
      label: `${i18n.t('EndTime')}`, field: 'notifyEndTime', minWidth: 80, search: false
    }, {
      label: `${i18n.t('Misc.ExceptionReason')}`, field: 'notifyExceptionMsg', minWidth: 80, search: false
    }, {
      label: `${i18n.t('TaskCore.NotificationObject')}`, field: 'callbackTargetType',
      options: notificationObjects.value, minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('TaskCore.TaskSource')}`, field: 'appId', minWidth: 80, search: false, hide: true
    }]
  }
})

const rowCls = ({ row }) => row.notifyStatus === '3' ? 'text-red-4' : ''
const rowDblClick = row => {
  rowInfo.value = row
  modalVisible.value = true
}
const handleLogs = row => {
  rowInfo.value = row
  logVisible.value = true
}
const handleManualNotify = ({ id = '' }) => {
  createDialog({ msg: `${i18n.t('TaskCore.ManualNotificationTips')}` }, () => {
    postAPI(`/yfagv/api/execL3TcrNotify/manualCall/${id}`, {}, () => startSearch(), () => {})
  })
}
const handleComplete = ({ id }) => {
  createDialog({ msg: `${i18n.t('TaskCore.ManualCompleteTips')}` }, () => {
    postAPI(`/yfagv/api/execL3TcrNotify/manualFinish/${id}`, {}, () => startSearch(), () => {})
  })
}
const handleJsonPopup = json => {
  jsonInfo.value = json
  popupVisible.value = !popupVisible.value
  targetEl.value = '.multipoint-params'
}
const formatSearchForm = () => {
  const params = { ...searchForm.value }
  if (props.taskCode) params.taskCode = props.taskCode
  if (searchForm.value.createdTime?.from) {
    params.createdStartTime = params.createdTime?.from
    params.createdEndTime = params.createdTime?.to
    delete params.createdTime
  }
  return { ...params }
}
const startSearch = (_, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
}
onMounted(startSearch)
</script>